package com.novel.base.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * SwaggerConfig
 *
 * @author 樊温军
 * @date 2025/3/19 下午2:37
 */
@Configuration
public class SwaggerConfig implements WebMvcConfigurer {

    //  配置了Swagger的Docket实例
    @Bean
    public Docket docket(Environment environment) {
        Profiles profiles = Profiles.of("dev", "uat");
        // 如果是dev、uat,这个地方的返回值就是true
        boolean enableSwagger = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(enableSwagger)
                .groupName(environment.getProperty("spring.application.name"));
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //添加默认的静态资源访问路径
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:static/", "classpath:META-IFA/resources/", "classpath:resources/", "classpath:public/", "classpath:/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}

